import { reactive, ref } from 'vue';
export const useAnnex = (props, emits) => {
    const attachmentData = reactive({
        list: [],
    });
    const proviewData = reactive({
        list: [],
    });
    const fileCurrent = ref({});
    const showPop = ref(false);

    /**
     * @description:  初始化附件数据
     * @param:
     * @returns: 获取到树状数据 外部--》大类--》小类--》file
     */
    const initAttachmentList = () => {
        const bigCategoryList = groupByBigCode(props.annexData);
        attachmentData.list = bigCategoryList.map(item => {
            const { subCategoryList } = item;
            const _list = groupBySubCode(subCategoryList);
            item.subCategoryList = _list;
            return item;
        });
        handleProviewList();
    };

    /**
     * @description:  获取大类
     */
    const groupByBigCode = list => {
        const _list = list.reduce((pre, cur) => {
            const { pCategoryCode, pCategoryName, categoryType, templateCode, templateName, ...data } = cur;
            if (pre[pCategoryCode]) {
                pre[pCategoryCode].subCategoryList.push({ pCategoryName, pCategoryCode, templateCode, categoryType, ...data });
            } else {
                pre[pCategoryCode] = {
                    templateCode,
                    templateName,
                    categoryType,
                    pCategoryName,
                    pCategoryCode,
                    subCategoryList: [{ pCategoryName, pCategoryCode, templateCode, categoryType, ...data }],
                };
            }
            return pre;
        }, {});
        return Object.values(_list);
    };
    /**
     * @description: 获取最终数据
     */
    const groupBySubCode = list => {
        const _list = list.reduce((pre, cur) => {
            const {
                categoryName,
                categoryCode,
                fileFormat,
                fileMaximum,
                fileMinimum,
                uploadOperation,
                par,
                fileId,
                downloadUrl,
                ...data
            } = cur;
            const _downloadUrl = downloadUrl && downloadUrl.indexOf('?') != -1 ? `${downloadUrl}&p=download` : `${downloadUrl}?p=download`;
            if (pre[categoryCode]) {
                if (fileId) {
                    pre[categoryCode].fileList.push({
                        categoryCode,
                        categoryName,
                        fileFormat,
                        fileId,
                        downloadUrl: _downloadUrl,
                        ...data,
                    });
                }
            } else {
                pre[categoryCode] = {
                    categoryCode,
                    categoryName,
                    fileFormat,
                    fileMaximum,
                    fileMinimum,
                    uploadOperation,
                    par,
                    fileList: fileId ? [{ categoryCode, categoryName, fileFormat, fileId, downloadUrl: _downloadUrl, ...data }] : [],
                };
            }
            return pre;
        }, {});
        return Object.values(_list);
    };

    /**
     * @description: 获取当前要打开的file
     */
    const openProview = file => {
        fileCurrent.value = file;
        showPop.value = true;
    };
    /**
     * @description:  关闭弹窗
     */
    const closeProview = () => {
        showPop.value = false;
    };
    /**
     * @description: 处理传入预览的数据
     * @returns 扁平化数组
     */
    const handleProviewList = () => {
        proviewData.list = [];
        attachmentData.list.map(subItem => {
            subItem.subCategoryList.map(fileItem => {
                fileItem.fileList.map(file => {
                    proviewData.list.push(file);
                });
            });
        });
    };
    /**
     * @description: 上传附件
     */
    const uploadSuccess = bigCategory => {
        const { pCategoryCode } = bigCategory;
        let pCategoryIndex = attachmentData.list.findIndex(item => item.pCategoryCode == pCategoryCode);
        attachmentData.list[pCategoryIndex] = bigCategory;
        handleProviewList();
        emits('uploadSuccess', attachmentData.list);
    };

    const deleteSuccess = bigCategory => {
        const { pCategoryCode } = bigCategory;
        let pCategoryIndex = attachmentData.list.findIndex(item => item.pCategoryCode == pCategoryCode);
        attachmentData.list[pCategoryIndex] = bigCategory;
        handleProviewList();
        emits('deleteSuccess', attachmentData.list);
    };

    return {
        initAttachmentList,
        attachmentData,
        openProview,
        fileCurrent,
        closeProview,
        showPop,
        handleProviewList,
        proviewData,
        uploadSuccess,
        deleteSuccess,
    };
};